//
//  SYS_OrderDetailVC.m
//  System_App
//
//  Created by 吴健 on 2017/11/30.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_OrderDetailVC.h"
#import "SYS_OrderDetailStatusView.h"
#import "SYS_OrderDetailAddressView.h"
#import "SYS_OrderDetailGoodsView.h"
#import "SYS_OrderDetailModel.h"
#import "SYS_ShipmentVC.h"

@interface SYS_OrderDetailVC ()<UIScrollViewDelegate>
{
    //发货按钮
    UIButton *btn_shipments;
    NSString *session;
    NSString *pid;
    
    SYS_OrderDetailModel *detailModel;
}
/**
 *  主视图scrollview
 */
@property (nonatomic, strong) UIScrollView *mainScrollView;
/**
 *  顶部一栏
 */
@property (nonatomic, strong) SYS_OrderDetailStatusView *headView;
/**
 *  地址
 */
@property (nonatomic, strong) SYS_OrderDetailAddressView *addressView;
/**
 *  商品
 */
@property (nonatomic, strong) SYS_OrderDetailGoodsView *goodsView;
@end

@implementation SYS_OrderDetailVC


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //隐藏导航栏顶部的黑线
    self.navigationController.navigationBar.subviews[0].subviews[1].hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBar];
    [self initWithData];
    [self setUI];
    [self getOrderInfoRequest];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- Setter and Getter
- (void)setNavigationBar{
    self.title = @"订单详情";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"SYS_Public_back" highImage:nil title:nil target:self action:@selector(backToPrevious)];
}

- (void)initWithData{
    session = [SYS_LoginModel instance].sessionInfo.session;
    UICKeyChainStore *keychainStore = [UICKeyChainStore keyChainStore];
    pid = [keychainStore stringForKey:@"pid"];
    detailModel = [SYS_OrderDetailModel new];
}

- (void)setUI{
    //当前布局高度
    float height=0;
    
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-64)];
    _mainScrollView.backgroundColor=RGBACOLOR(242, 242, 242, 1);
    _mainScrollView.delegate=self;
    _mainScrollView.showsHorizontalScrollIndicator=NO;
    _mainScrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_mainScrollView];
    
    //头部状态
    _headView = [[SYS_OrderDetailStatusView alloc] initWithFrame:CGRectMake(0, height, SCREENWIDTH, 100)];
    [_mainScrollView addSubview:_headView];
    height+=100;
    
    //地址信息
    _addressView = [[SYS_OrderDetailAddressView alloc] initWithFrame:CGRectMake(0, height, SCREENWIDTH, 90)];
    [_mainScrollView addSubview:_addressView];
    
    //商品
    _goodsView = [[SYS_OrderDetailGoodsView alloc] init];
//    _goodsView.delegate=self;
    //    _goodsView.goodArr=@[@"1",@"2"];
    _goodsView.status = nil;
    [_mainScrollView addSubview:_goodsView];
    
    _goodsView.sd_layout
    .leftSpaceToView(_mainScrollView, 0)
    .rightSpaceToView(_mainScrollView, 0)
    .topSpaceToView(_addressView, 10)
    .autoHeightRatio(0);
    
    btn_shipments = [UIButton new];
//    btn_shipments.hidden = YES;
    [btn_shipments setTitle:@"发货" forState:UIControlStateNormal];
    [btn_shipments setTitleColor:DSCOLOR_WHITE forState:UIControlStateNormal];
    [btn_shipments setBackgroundColor:DSCOLOR_BLUE];
    [btn_shipments addTarget:self action:@selector(doShipmentsAction:) forControlEvents:UIControlEventTouchUpInside];
    [_mainScrollView addSubview:btn_shipments];
    
    btn_shipments.sd_layout
    .leftSpaceToView(_mainScrollView, 15)
    .rightSpaceToView(_mainScrollView, 15)
    .topSpaceToView(_goodsView, 15)
    .heightIs(40);
    
    btn_shipments.layer.masksToBounds = YES;
    btn_shipments.layer.cornerRadius  = 20.0f;
    
    [_mainScrollView setupAutoHeightWithBottomView:btn_shipments bottomMargin:10];
}

//刷新UI
- (void)refreshUI{
    _addressView.lb_name.text = FORMAT(@"收货人：%@", detailModel.shippingAddressName);
    _addressView.lb_phone.text = detailModel.shippingAddressContact;
    _addressView.lb_address.text = detailModel.shippingAddress;
    
    _goodsView.detailModel = detailModel;
}

#pragma mark -- Touch and Event
- (void)backToPrevious{
    [self.navigationController popViewControllerAnimated:YES];
}

//发货按钮
- (void)doShipmentsAction:(UIButton *)sender{
    SYS_ShipmentVC *vc = [[SYS_ShipmentVC alloc] initWithOrderId:_orderId];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- HttpRequest
//获取订单详情接口数据
- (void)getOrderInfoRequest{
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:session forKey:@"session"];
    [params setValue:pid forKey:@"pid"];
    [params setValue:_orderId forKey:@"orderId"];
    [SYS_HttpUtility getOrderInfoRequest:params success:^(id returnValue) {
        if ([returnValue[@"flag"] isEqualToString:@"true"]) {
            NSDictionary *dic = returnValue[@"data"];
            detailModel = [SYS_OrderDetailModel mj_objectWithKeyValues:dic];
            NSMutableArray *mar_item = [NSMutableArray new];
            for (int i = 0; i < detailModel.orderItems.count; i++) {
                OrderItems *itemModel = [OrderItems mj_objectWithKeyValues:detailModel.orderItems[i]];
                [mar_item addObject:itemModel];
            }
            detailModel.orderItems = mar_item;
            [self refreshUI];
        }else{
            [DSUtility mbSay:returnValue[@"exp"]];
        }
    } failure:^{
        RequestFailure;
    }];
}


@end
